package com.jxdinfo.hussar.cloud.module.example.dljc.basketdataalarm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdataalarm.dao.BasketDataAlarmMapper;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdataalarm.model.BasketDataAlarm;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdataalarm.service.BasketDataAlarmService;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdataalarm.vo.BasketDataAlarmSearchVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketdataalarm.vo.BasketDataAlarmVO;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 吊篮 报警数据服务实现类
 */
@Service("dljc.basketdataalarm.BasketDataAlarmServiceImpl")
@HussarDs("master")
public class BasketDataAlarmServiceImpl extends HussarServiceImpl<BasketDataAlarmMapper, BasketDataAlarm> implements BasketDataAlarmService {

    private static final Logger logger = LoggerFactory.getLogger(BasketDataAlarmServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private BasketDataAlarmMapper basketDataAlarmMapper;

    @Override
    public ApiResponse<List<BasketDataAlarmVO>> findByList(BasketDataAlarmSearchVO basketDataAlarmSearchVO) {
        QueryWrapper<BasketDataAlarm> wrapper = new QueryWrapper<>();

        if(ObjectUtils.isNotEmpty(basketDataAlarmSearchVO.getUnloadId())) {
            wrapper.eq("unload_id", basketDataAlarmSearchVO.getUnloadId());
        }
        if(ObjectUtils.isNotEmpty(basketDataAlarmSearchVO.getDataStatus())) {
            wrapper.eq("data_status", basketDataAlarmSearchVO.getDataStatus());
        }
        if(ObjectUtils.isNotEmpty(basketDataAlarmSearchVO.getDataType())) {
            wrapper.eq("data_type", basketDataAlarmSearchVO.getDataType());
        }
        if(ObjectUtils.isNotEmpty(basketDataAlarmSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code", basketDataAlarmSearchVO.getTyProjectCode());
        }
        if(ObjectUtils.isNotEmpty(basketDataAlarmSearchVO.getCollectionTimeStart())) {
            wrapper.ge("collection_time", basketDataAlarmSearchVO.getCollectionTimeStart());
        }
        if(ObjectUtils.isNotEmpty(basketDataAlarmSearchVO.getCollectionTimeEnd())) {
            wrapper.le("collection_time", basketDataAlarmSearchVO.getCollectionTimeEnd());
        }
        if(ObjectUtils.isNotEmpty(basketDataAlarmSearchVO.getUnloadIds())) {
            List<String> stringList = Arrays.asList(basketDataAlarmSearchVO.getUnloadIds().split(","));
            wrapper.in("unload_id", stringList);
        }

        wrapper.orderByDesc("ty_create_time");

        List<BasketDataAlarm> basketDataAlarms = basketDataAlarmMapper.selectList(wrapper);
        List<BasketDataAlarmVO> basketDataAlarmVOS = BeanUtil.copyToList(basketDataAlarms, BasketDataAlarmVO.class);

        return ApiResponse.success(basketDataAlarmVOS);
    }
}
